package controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import mapper.TbItemMapper;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.TbItem;
import pojo.TbItemExample;

import java.util.List;

public class TestPageHelper {

    /**
     * 测试PageHelper
     */
    @Test
    public void testPageHelper() {
        //创建一个spring容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
        //从spring容器中获得Mapper的代理对象
        TbItemMapper tbItemMapper = applicationContext.getBean(TbItemMapper.class);
        //执行查询，并分页
        TbItemExample tbItemExample = new TbItemExample();
        //分页处理
        PageHelper.startPage(2,10);
        List<TbItem> list = tbItemMapper.selectByExample(tbItemExample);
        //取商品列表
        for (TbItem tbItem : list){
            System.out.println(tbItem.getTitle());
        }
        //取分页信息
        PageInfo<TbItem> pageInfo = new PageInfo<TbItem>(list);
        long total = pageInfo.getTotal();
        System.out.println("共有商品："+total);
    }
}
